package com.example.studentalarm.presenter;


import android.content.Context;
import android.os.Handler;

import com.example.studentalarm.bean.TimetableBean;
import com.example.studentalarm.calendar.RepayPlanInfo;
import com.example.studentalarm.database.DbManager;
import com.example.studentalarm.utils.LogUtil;
import com.example.studentalarm.utils.TimeUtils;
import com.example.studentalarm.view.ITimetableView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 主mainActivity的P层
 */
public class TimetableMvpPresenter extends BasePresenter<ITimetableView> {

    public TimetableMvpPresenter(Context mContext) {
        super(mContext);
    }

    /***
     * 获取课程表数据
     */
    public void requestTimetable() {
        List<TimetableBean> timetableList = DbManager.getInstance().queryTimetableList();
        if (isViewAttached()) {
            if (timetableList != null && timetableList.size() > 0) {
                getView().getTimetableList(timetableList);
            } else {
                getView().showEmptyList("暂无相关信息");
            }
        }
    }

    /***
     * 获取课程表数据
     */
    public void requestTimetableBydate(String date) {
        List<TimetableBean> timetableList = DbManager.getInstance().queryTimetableListByDate(date);
        if (isViewAttached()) {
            if (timetableList != null && timetableList.size() > 0) {
                getView().getTimetableList(timetableList);
            } else {
                getView().showEmptyList("暂无相关信息");
            }
        }
    }

    /***
     *删除课程表
     * @param id
     */
    public void deleteTimetable(Integer id) {
        DbManager.getInstance().deleteTimetable(id);

    }


    /**
     * 获取日历提醒数据
     */
    public ArrayList<RepayPlanInfo> getCalendarRemindData() {
        Map<String, Integer> map = new HashMap<>();
        List<TimetableBean> timetableList = DbManager.getInstance().queryTimetableList();
        ArrayList<RepayPlanInfo> data = new ArrayList<>();
        for (TimetableBean bean : timetableList) {
            if (!map.containsKey(bean.getDate())) {
                map.put(bean.getDate(), 1);
            }else{
                Integer nextRemindNum = map.get(bean.getDate()) + 1;
                map.put(bean.getDate(), nextRemindNum);
            }
        }

        for (String key : map.keySet()) {
            Integer remind = map.get(key);
            LogUtil.d("提醒数据 = "+key+"  提示数量 = "+remind);
            Calendar time = TimeUtils.stringToDate(key, TimeUtils.PATTERN);
            data.add(new RepayPlanInfo(time.get(Calendar.YEAR), time.get(Calendar.MONTH)+1, time.get(Calendar.DATE), remind));
        }
        return data;
    }
}